package com.it.smn.divide.loadbalance;

import com.it.smn.divide.datasource.AbstractDataSourceWrapper;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author sunmannian
 * @ClassName com.it.smn.divide.loadbalance.LoadBalanceStrategy
 * @Date 2020/8/27 18:27
 * @Version V1.0
 * Description  <负载均衡策略>
 * Copyright (C) 北京中电兴发科技有限公司  since 2020/8/27
 */
public interface LoadBalanceStrategy {


    int ONLY_ONE = 1;

    /**
     * <描述方法功能>
     * <选择一个数据源>
     *
     * @param dataSources
     * @return javax.sql.DataSource
     * @Author sunmannian
     * @Description
     * @Date 2020/8/27 19:36
     **/
    DataSource select(List<DataSource> dataSources);

    /**
     * <描述方法功能>
     * <获取数据源的权重>
     *
     * @param dataSource
     * @return int
     * @Author sunmannian
     * @Description
     * @Date 2020/8/27 18:46
     **/
    default int getWeight(DataSource dataSource) {
        if (dataSource instanceof AbstractDataSourceWrapper) {
            return ((AbstractDataSourceWrapper) dataSource).getWeight();
        }
        return 0;
    }
}
